package cn.nebula.music.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.nebula.music.domain.Songlist;
import cn.nebula.music.service.SongService;
import cn.nebula.music.service.SonglistService;
import cn.nebula.music.service.UserService;

@Controller
public class SonglistController {

	@Autowired
	private SonglistService songlistService;

	@Autowired
	private SongService songService;
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/editSonglist")
	@ResponseBody
	public Map<String,Object> editSonglist(@RequestParam Integer id) throws Exception {
		Map<String,Object> values = new HashMap<String,Object>();
		values.put("songlist", songlistService.findSonglistById(id));
		values.put("songs",songService.findAllSongsOfBT());
		values.put("users", userService.findAllUsersOfBT());
		return values;
	}
	
	@RequestMapping("/listAllSonglists")
	@ResponseBody public Map<String, Object> listAllSonglists() throws Exception {
		Map<String,Object> values = new HashMap<String, Object>();
		values.put("songlists",songlistService.findAllSonglistsOfBT());
		return values;
	}
	
	@RequestMapping("/newSonglist")
	@ResponseBody
	public Map<String,Object> newSonglist() throws Exception {
		Map<String,Object> values = new HashMap<>();
		values.put("songs",songService.findAllSongsOfBT());
		values.put("users", userService.findAllUsersOfBT());
		return values;
	}
	
	@RequestMapping("/updateSonglist")
	@ResponseBody
	public boolean updateSonglist(@RequestBody Songlist songlist) throws Exception {
		songlistService.updateSonglist(songlist);
		return true;
	}
	
	@RequestMapping("/addSonglist")
	@ResponseBody
	public boolean addSonglist(@RequestBody Songlist songlist) throws Exception {
		songlistService.addSonglist(songlist);
		return true;
	}
	
	@RequestMapping("/deleteSonglist")
	@ResponseBody
	public boolean deleteSonglist(@RequestParam Integer id) throws Exception {
		songlistService.deleteSonglistById(id);
		return true;
	}
}
